package com.ctrlcar.giftrecord.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctrlcar.giftrecord.dto.ReceiveGiftDTO;
import com.ctrlcar.giftrecord.mapper.RelativeMapper;
import com.ctrlcar.giftrecord.pojo.GiftBook;
import com.ctrlcar.giftrecord.pojo.ReceiveGift;
import com.ctrlcar.giftrecord.pojo.Relative;
import com.ctrlcar.giftrecord.pojo.model.Result;
import com.ctrlcar.giftrecord.service.GiftBookService;
import com.ctrlcar.giftrecord.service.ReceiveGiftService;
import com.ctrlcar.giftrecord.service.RelativeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:
 * @Description: 收礼表操作
 * @DateTime: 2025-03-27 9:39
 **/


import javax.annotation.Resource;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/receive-gifts")
public class ReceiveGiftController {

    @Autowired
    private ReceiveGiftService receiveGiftService;


    /**
     * 获取所有接收礼物的记录
     * @return
     */
    @GetMapping("/list")
    public Result<List<ReceiveGift>> getAllReceiveGifts(@RequestParam Integer userId) {
        LambdaQueryWrapper<ReceiveGift> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReceiveGift::getUserId, userId);
        List<ReceiveGift> list = receiveGiftService.list(queryWrapper);
        return Result.success(list);
    }


    /**
     * 根据ID获取接收礼物的详细信息
     * @param receiveId
     * @return
     */
    @GetMapping("/getById")
    public Result<ReceiveGift> getReceiveGiftById(@RequestParam Integer receiveId) {
        return Result.success( receiveGiftService.getById(receiveId));
    }

    /**
     *  创建 收礼记录
     * @param receiveGiftDTO
     * @return
     */
    @PostMapping
    public Result createReceiveGift(@RequestBody ReceiveGiftDTO  receiveGiftDTO) {
        Result res =  receiveGiftService.addReceiveGift(receiveGiftDTO);
        return  res;
    }

    /**
     *  删除指定ID的接收礼物记录
     * @param
     * @return
     */
    @DeleteMapping
    public Result deleteReceiveGift(@RequestParam Integer receiveId) {
        boolean deleted = receiveGiftService.removeById(receiveId);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 修改送礼记录
     * @param receiveGift
     * @return
     */
    @PutMapping
    public Result  updateReceiveGift(@RequestBody ReceiveGift receiveGift){
        boolean update = receiveGiftService.updateById(receiveGift);
        if (update){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }
    /**
     * 根据亲朋ID获取接收礼物的记录
     * @param userId
     * @return
     */
    @GetMapping("/listByRelativeIs")
    public Result<List<ReceiveGift>> getReceiveGiftsByRelativeIs(@RequestParam Integer userId, @RequestParam Integer relativeId) {
        LambdaQueryWrapper<ReceiveGift> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null ){
            queryWrapper.eq(ReceiveGift::getUserId, userId);
        }
        queryWrapper.eq(ReceiveGift::getRelativeId, relativeId);
        List<ReceiveGift> list = receiveGiftService.list(queryWrapper);
        return Result.success(list);
    }
}
